R Graph Gallery
Must Watch!
MustWatch
https://www.r-graph-gallery.com/index.html
R Graph Gallery
Welcome the R graph gallery, a collection of charts made with the R programming language.
Hundreds of charts are displayed in several sections, always with their reproducible code available. The gallery makes a focus on the
tidyverse and ggplot2. Feel free to suggest a chart or report a bug; any
feedback is highly welcome. Stay in touch with the gallery by following it on Twitter
or Github. If you're new to R,
consider following thiscourse.
Distribution
Correlation
Ranking
Part of a whole
Evolution
Map
Flow
General knowledge
ggplot2
ggplot2 is the most popular alternative to base R graphics.
It is based on the Grammar of Graphics and its main advantage is its flexibility, as you can create and customize the graphics adding more layers to it.
This library allows creating ready-to-publish charts easily
CUSTOMIZATION
The ggplot2 package allows customizing the charts with themes.
It is possible to customize everything of a plot, such as the colors, line types, fonts, alignments, among others, with the components of the theme
function.
In addition, there are several functions you can use to customize the graphs adding titles, subtitles, lines, arrows or texts.
Title, subtitle, caption and tag
Text annotations
Background color
Grid customization
Margins
Themes
Legends
Reference lines, segments, curves and arrows
Coordinate systems
Parliament diagram with ggparliament
Adding dogs to ggplot2 with ggdogs
Diverging bar chart
Sankey diagrams with ggsankey
Cartograms
Proportional symbol maps
Choropleth maps
Ridgeline plot with ggridges
Beeswarm with ggbeeswarm
Streamgraph
Slopegraph
Treemaps with treemapify
Alluvial plot with ggalluvial
Waterfall charts with waterfalls package
Heat map
Area chart with geom_area
Line graph with multiple lines
Lollipop chart
# Sample data set
set.seed(1)
df <- data.frame(x = LETTERS[1:10],
y = sample(20:35, 10, replace = TRUE))
library(ggplot2)
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y)) +
geom_point()
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y)) +
geom_point() +
coord_flip()
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y)) +
geom_point(size = 4, pch = 21, bg = 4, col = 1) +
coord_flip()
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y),
color = "gray", lwd = 1.5) +
geom_point(size = 4, pch = 21, bg = 4, col = 1) +
coord_flip()
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y),
color = "gray", lwd = 1) +
geom_point(size = 4, pch = 21, bg = 4, col = 1) +
scale_x_discrete(labels = paste0("G_", 1:10)) +
coord_flip()
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y),
color = "gray", lwd = 1) +
geom_point(size = 4, pch = 21, bg = 4, col = 1) +
scale_x_discrete(labels = paste("Group", 1:10)) +
theme(axis.text.x = element_text(angle = 90,
vjust = 0.5, hjust = 1))
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y),
color = "gray", lwd = 1) +
geom_point(size = 4, pch = 21, bg = 4, col = 1) +
scale_x_discrete(labels = paste0("G_", 1:10)) +
coord_flip() +
theme_minimal()
ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(x = x, xend = x, y = 0, yend = y),
color = "gray", lwd = 1) +
geom_point(size = 7.5, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = y), color = "white", size = 3) +
scale_x_discrete(labels = paste0("G_", 1:10)) +
coord_flip() +
theme_minimal()
ggplot(df, aes(x = reorder(x, -y), y = y)) +
geom_segment(aes(x = reorder(x, -y),
xend = reorder(x, -y),
y = 0, yend = y),
color = "gray", lwd = 1) +
geom_point(size = 4, pch = 21, bg = 4, col = 1) +
xlab("Group") +
ylab("") +
coord_flip() +
theme_minimal()
Pairs plot with ggpairs
2D density contour plots
Hexbin chart
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex()
ggplot(df, aes(x = x, y = y)) +
geom_hex(bins = 15)
ggplot(df, aes(x = x, y = y)) +
geom_hex(bins = 60)
ggplot(df, aes(x = x, y = y)) +
geom_hex(color = "white")
ggplot(df, aes(x = x, y = y)) +
geom_hex(color = 1, fill = 4, alpha = 0.4)
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
scale_fill_viridis_c()
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
guides(fill = guide_colourbar(barwidth = 0.7, barheight = 15))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
guides(fill = guide_colourbar(title = "Count"))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
guides(fill = guide_colourbar(label = FALSE,
ticks = FALSE))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
theme(legend.position = "none")
Parallel coordinates with ggparcoord
Drawing functions with geom_function
Venn diagram
Connected scatter plot
Scatter plot with ellipses
Density plot by group
Voronoi diagram with ggvoronoi
Waffle chart (square pie)
Bump chart with ggbump
install.packages("tidyverse")
install.packages("ggbump")
library(tidyverse)
library(ggbump)
year <- rep(2019:2021, 4)
position <- c(4, 2, 2, 3, 1, 4, 2, 3, 1, 1, 4, 3)
player <- c("A", "A", "A","B", "B", "B", "C", "C", "C","D", "D", "D")
df <- data.frame(x = year, y = position, group = player)
ggplot(df, aes(x = x, y = y, color = group)) +
geom_bump()
ggplot(df, aes(x = x, y = y, color = group)) +
geom_bump(size = 1.5) +
geom_point(size = 6)
ggplot(df, aes(x = x, y = y, color = group)) +
geom_bump(size = 1.5) +
geom_point(size = 6) +
scale_color_brewer(palette = "RdBu")
ggplot(df, aes(x = x, y = y, color = group)) +
geom_bump(size = 1.5) +
geom_point(size = 6) +
geom_text(data = df %>% filter(x == min(x)),
aes(x = x - 0.1, label = group),
size = 5, hjust = 1) +
geom_text(data = df %>% filter(x == max(x)),
aes(x = x + 0.1, label = group),
size = 5, hjust = 0) +
scale_color_brewer(palette = "RdBu") +
theme_void() +
theme(legend.position = "none")
Radar chart with ggradar
Density plot with geom_density
Line graph
Violin plot by group
Donut chart
Pie chart
Pie chart with labels outside
Pie chart with percentages
Stacked bar chart
Violin plot with data points
Violin plot with mean
Scatter plot with marginal histograms
Anatogram images with gganatogram
Scatter plot by group
Scatter plot
Adding Bernie Sanders to ggplot2
Histogram with Sturges method
LEGO mosaics in R with brickr
Plotting brain atlases with ggseg
Adding cats to ggplot2 with ggcats
Adding emojis to ggplot2 with emoGG
Game Boy screen simulator with ggboy
geom_lime and geom_pint
Histogram bins and binwidth
Histogram by group
Histogram with density
Box plot with jittered data points
Yearly calendar heat map in R
Box plot
Monthly calendar heat map in R
Yearly calendar
Box plot by group
Game boy
Keyboards with ggkeyboard
Lunar calendar with ggplot2
Monthly calendar
Soccer event data with ggsoccer